/**
 * 
 */

cc.Class({
    extends: cc.Component,

    properties: {
        icon:cc.Sprite,
        nickName:cc.Label,
        data:cc.Label,
        rank:cc.Label,
        rankImg:cc.Sprite,
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {

    },
    setData(iconUrl, userName, data, rank){
        //name
        this.nickName.string  = userName; 
        //data
        this.data.string = data;
        if(rank && rank != -1){
            if(this.rank)
                this.rank.string = ""+rank;
        }else {
            if(this.rank)
                this.rank.string = "未上榜";
        }
        var _self = this;
        cc.loader.load({
            url: iconUrl, type: 'jpg'
            }, (err, texture) => {
                if (err) {
                    console.error("下载头像失败：" + err);
                    return;
                }
                
                _self.icon.spriteFrame = new cc.SpriteFrame(texture);
            }
        );   
        if(this.rank && this.rankImg){
            //前三名
            if(rank >= 1 && rank <= 3){
                this.rankImg.node.active = true;
                this.rank.node.active = false;
                var str = "";
                if(rank == 1){
                    str = "first";
                }else if(rank == 2){
                    str = "second";
                }else if(rank == 3){
                    str = "third";
                }
                cc.loader.loadRes(str, cc.SpriteFrame, function (err, spriteFrame) {
                    if (err) {
                        cc.error(err.message || err);
                        return;
                    }
                    this.rankImg.spriteFrame = spriteFrame;
                }.bind(this)); 
            }else {
                this.rankImg.node.active = false;
                this.rank.node.active = true;
            }
        }
    },
    // update (dt) {},
});
